<?php
class ModuleService extends Service {
    
    function __construct() {
        parent::__construct();
        $this->mDao = new ModuleDao();
    }
    
    public function insertObject($obj){
        try {
            $newId = str_replace(" ", "_", strtolower($obj->getName()));
            $obj->setId($newId);
            if($this->mDao->getObject($obj->getId())!=null){
                throw new Exception("Entry with id [$newId] is Exist.");
            }

            return $this->mDao->insertObject($obj);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }

    public function updateObject($obj,$oldId){
        try {
            $this->mDao->beginTrans();
            
                $newId = str_replace(" ", "_", strtolower($obj->getName()));
                $obj->setId($newId);
                if($obj->getId()!=$oldId){
                    if($this->mDao->getObject($obj->getId())!=null){
                        throw new Exception("Duplicate entry for id [$newId].");
                    }
                }
                if($this->mDao->updateObject($obj,$oldId)==null) return null;
                $FunctionsService = new FunctionsService();
                if($FunctionsService->updateModuleId($obj->getId(), $oldId)==false) return false;
                
            $this->mDao->commitTrans();
            
            return true;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            $this->mDao->roolback();
        }
    }
    
    public function deleteObject($id){
        try {
            return $this->mDao->deleteObject($id);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }

    
    public function getList() {
        try {
            return $this->mDao->getList();
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
    public function getObject($id){
        try {
            return $this->mDao->getObject($id);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
}
?>